﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="New.aspx.cs" Inherits="MonkeyWarranty.Web.InstantTickets.New" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <title>
        <%=Resources.GlobalStrings.InstantTickets %></title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        <%=Resources.GlobalStrings.InstantTicketForm%> - <%=Resources.GlobalStrings.InstantTickets %></h2>
    <p>
    </p>
    <span class="failureNotification">
        <asp:Literal ID="lblErrorMessage" runat="server"></asp:Literal>
    </span>
    
    <!-- Customer -->
    <div>
        <fieldset class="register">
            <legend><%=Resources.GlobalStrings.Customers%></legend>
            <label style="display: inline;"><asp:RadioButton ID="rbtNewCustomer" runat="server" GroupName="customer" Checked="True" />Nhap moi khach hang</label>
            <label style="display: inline;"><asp:RadioButton ID="rbtSelectCustomer" runat="server" GroupName="customer" />Chon tu danh sach khach hang</label>
            <p></p>
            <asp:HiddenField ID="hidCustomerId" runat="server" />
            <%=Resources.GlobalStrings.Name %><asp:TextBox ID="txtName" runat="server" CssClass="textEntry name"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtName"
                        CssClass="failureNotification" ValidationGroup="AddNew">(*)</asp:RequiredFieldValidator>
            <%=Resources.GlobalStrings.Phone %><asp:TextBox ID="txtPhone" runat="server" CssClass="textEntry name"></asp:TextBox>
            <%=Resources.GlobalStrings.Address %><asp:TextBox ID="txtAddress" runat="server" CssClass="textEntry name"></asp:TextBox>
            <%=Resources.GlobalStrings.Email %><asp:TextBox ID="txtEmail" runat="server" CssClass="textEntry name"></asp:TextBox>
        </fieldset>
    </div>

    <!-- Common information -->
    <div>
        <fieldset class="register">
            <legend><%=Resources.GlobalStrings.Information %></legend>
            <%=Resources.GlobalStrings.TicketNumber %>
            <asp:TextBox ID="txtCodePrefix" runat="server" Enabled="False" Text="TN" CssClass="textEntry codeprefix"></asp:TextBox> - 
            <asp:TextBox ID="txtCode" runat="server" Enabled="False" CssClass="textEntry codeprefix"></asp:TextBox>
            <%=Resources.GlobalStrings.ReceivedDate %><asp:TextBox ID="txtReceivedDate" runat="server" CssClass="textEntry date"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtReceivedDate"
                        CssClass="failureNotification" ValidationGroup="AddNew">(*)</asp:RequiredFieldValidator>
            <%=Resources.GlobalStrings.ReceivedEmployee %><asp:DropDownList ID="ddlEmployees" runat="server"></asp:DropDownList>
            <p></p>
            <%=Resources.GlobalStrings.Content %> 
            <asp:TextBox ID="txtContent" runat="server" CssClass="textEntry content"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtContent"
                        CssClass="failureNotification" ValidationGroup="AddNew">(*)</asp:RequiredFieldValidator>
            <%=Resources.GlobalStrings.Distance %> 
            <asp:TextBox ID="txtDistance" runat="server" CssClass="textEntry codeprefix" Text="0,00"></asp:TextBox>
        </fieldset>
    </div>
    <!-- End common -->
    
    <div id="customers-dialog-form" title="<%=Resources.GlobalStrings.CustomersList %>">
        <div class="filtering">
            <label>
                <%=Resources.GlobalStrings.Name %>:
                <input type="text" name="name" id="name" /></label>
            <label>
                <%=Resources.GlobalStrings.CustomerGroups %>:
                <asp:DropDownList ID="ddlCustomerGroups" runat="server">
                </asp:DropDownList>
            </label>
            <button type="submit" id="LoadRecordsButton"><%=Resources.GlobalStrings.LoadRecords %></button>
        </div>
        <div id="CustomersTableContainer"></div>
        <p><a href="../Customers/New.aspx" target="_blank"><%=Resources.GlobalStrings.AddNew %></a></p>
    </div>
    <!-- End Customer -->
    
    <p>
    </p>
    <div>
        <input id="btnCancel" type="button" value="<%=Resources.GlobalStrings.Cancel %>"
            class="submitButton" onclick="javascript:window.location='List.aspx';" />
        <asp:Button ID="btnSave" runat="server" Text="<%$ Resources:GlobalStrings, Save %>"
            CssClass="submitButton" ValidationGroup="AddNew" OnClick="btnSave_Click" />
    </div>

    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%=txtReceivedDate.ClientID %>").calendar();
            $("#tabs").tabs();

            $("#dialog:ui-dialog").dialog("destroy");
            $("#customers-dialog-form").dialog({
                autoOpen: false,
                height: 400,
                width: 800,
                modal: true
            });

            $("#<%=rbtNewCustomer.ClientID %>").click(function () {
                NewCustomer();
            });
            $("#<%=rbtSelectCustomer.ClientID %>").click(function () {
                SelectCustomer();
            });
        });

        //Prepare jtable plugin
        $('#CustomersTableContainer').jtable({
            //title: '<%=Resources.GlobalStrings.CustomersList %>',
            paging: true,
            pageSize: 10,
            sorting: true,
            defaultSorting: 'Name ASC',
            actions: {
                listAction: '../Customers/List.aspx/CustomerListByFilter'
            //createAction: 'List.aspx/CreateCustomer',
            //updateAction: 'List.aspx/UpdateCustomer',
            //deleteAction: 'List.aspx/DeleteCustomer'
            },
            fields: {
                Id: {
                    key: true,
                    create: false,
                    edit: false,
                    list: false
                },
                Code: {
                    title: '<%=Resources.GlobalStrings.Code %>',
                    //defaultValue: 'KH',
                    //inputClass: 'validate[required]',
                    list: false
                },
                Name: {
                    title: '<%=Resources.GlobalStrings.Name %>',
                    width: '20%',
                    //inputClass: 'validate[required]'
                    display: function (data) {
                        return '<a href="javascript:void(0);" onclick="javascript:CustomerSelect({Id:' + data.record.Id +
                            ',Name:\'' + data.record.Name + '\'' +
                                ',Phone:\'' + data.record.Phone + '\'' +
                                    ',Email:\'' + data.record.Email + '\'' +
                                        ',Address:\'' + data.record.Address + '\'' +
                                            '});">' + data.record.Name + '</a>';
                    }
                },
                Phone: { title: '<%=Resources.GlobalStrings.Phone %>', sorting: false },
                Email: { title: '<%=Resources.GlobalStrings.Email %>', sorting: false },
                Address: { title: '<%=Resources.GlobalStrings.Address %>', sorting: false }
            },
            //Initialize validation logic when a form is created
            formCreated: function (event, data) {
                data.form.validationEngine();
            },
            //Validate form when it is being submitted
            formSubmitting: function (event, data) {
                return data.form.validationEngine('validate');
            },
            //Dispose validation logic when form is closed
            formClosed: function (event, data) {
                data.form.validationEngine('hide');
                data.form.validationEngine('detach');
            }
        });

        //Re-load records when user click 'load records' button.
        $('#LoadRecordsButton').click(function (e) {
            e.preventDefault();
            $('#CustomersTableContainer').jtable('load', {
                name: $('#name').val(),
                customerGroupId: $('#<% = ddlCustomerGroups.ClientID %>').val()
            });
        });

        //Load all records when page is first shown
        //$('#LoadRecordsButton').click();
    </script>
    
    <script type="text/javascript">
        function CustomerSelect(customer) {
            $("#customers-dialog-form").dialog("close");
            //console.debug(customer);
            $("#<%=hidCustomerId.ClientID %>").val(customer.Id);
            $("#<%=txtName.ClientID %>").val(customer.Name);
            $("#<%=txtAddress.ClientID %>").val(customer.Address);
            $("#<%=txtPhone.ClientID %>").val(customer.Phone);
            $("#<%=txtEmail.ClientID %>").val(customer.Email);
        }
        
        function NewCustomer() {
            //$("#customers-dialog-form").dialog("close");
            $("#<%=txtName.ClientID %>").prop("disabled", false);
            $("#<%=txtAddress.ClientID %>").prop("disabled", false);
            $("#<%=txtPhone.ClientID %>").prop("disabled", false);
            $("#<%=txtEmail.ClientID %>").prop("disabled", false);
            $("#<%=txtName.ClientID %>").val('');
            $("#<%=txtAddress.ClientID %>").val('');
            $("#<%=txtPhone.ClientID %>").val('');
            $("#<%=txtEmail.ClientID %>").val('');
            $("#<%=txtName.ClientID %>").focus();
        }

        function SelectCustomer() {
            $("#<%=hidCustomerId.ClientID %>").val(0);
            $("#<%=txtName.ClientID %>").val('');
            $("#<%=txtAddress.ClientID %>").val('');
            $("#<%=txtPhone.ClientID %>").val('');
            $("#<%=txtEmail.ClientID %>").val('');
            // read only customer data
            $("#<%=txtName.ClientID %>").prop("disabled", true);
            $("#<%=txtAddress.ClientID %>").prop("disabled", true);
            $("#<%=txtPhone.ClientID %>").prop("disabled", true);
            $("#<%=txtEmail.ClientID %>").prop("disabled", true);
            // load data
            $("#customers-dialog-form").dialog("open");
            $('#LoadRecordsButton').click();
        }
    </script>
</asp:Content>
